package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import classes.Poslovnica;
import classes.Market;
import classes.Proizvod;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class PoslovnicaDAO {
	
	@SuppressWarnings("unused")
	private final Connection c;
	private final PreparedStatement qInsert, qSelectAll, qDelete, qSelectPoslovnica;
	private static WebResource service;
	
	public PoslovnicaDAO(Connection c) throws Exception {
		this.c = c;
		qInsert= c.prepareStatement("INSERT INTO poslovnica(idPoslovnica, naziv, lokacija, Market_idMarket) VALUES(null, ?, ?, ?)");
		qSelectAll = c.prepareStatement("SELECT * FROM poslovnica");
		qDelete = c.prepareStatement("DELETE FROM poslovnica WHERE idPoslovnica=?");
		qSelectPoslovnica = c.prepareStatement("SELECT * FROM poslovnica WHERE idPoslovnica = ?");
	}
	
	public int insertPoslovnica(Poslovnica p) throws Exception {
		if (p==null)
			throw new Exception("Nije moguce dodati nepostojeci proizvod!");
		qInsert.setString(1, p.getNaziv());
		qInsert.setString(2, p.getLokacija());
		qInsert.setInt(3, p.getMarket().getIdMarket());
		try{
			int r= qInsert.executeUpdate();
			return r;
		}
		catch(Exception ex){
			throw new Exception("Greska kod dodavanja vrste!");
		}
	}

	public Poslovnica selectPoslovnica(int id) throws Exception{
		qSelectPoslovnica.setInt(1, id);
		ResultSet rs = qSelectPoslovnica.executeQuery();
		if(rs.next()==false)
			throw new Exception("Vratio null");
		Poslovnica p = new Poslovnica();
		p.setIdPoslovnica(rs.getInt("idPoslovnica"));
		p.setNaziv(rs.getString("naziv"));
		p.setLokacija(rs.getString("lokacija"));
		p.setMarket(DAOFactory.getMarketDAO().selectMarket(rs.getInt("Market_idMarket")));
		return p;
	}
	
	public List<Poslovnica> selectSvePoslovnice() throws Exception{
		ResultSet rs = qSelectAll.executeQuery();
		List<Poslovnica> poslovnice = new ArrayList<Poslovnica>();
	//	ClientConfig config = new DefaultClientConfig();
	//    Client client = Client.create(config);
	//	service = client.resource(UriBuilder.fromUri("http://localhost:8080/eKupovina").build());
		while (rs.next()){
			Poslovnica p= new Poslovnica();
			p.setIdPoslovnica(rs.getInt("idPoslovnica"));
			p.setNaziv(rs.getString("naziv"));
			p.setLokacija(rs.getString("lokacija"));
		//	Vrsta vrsta= service.path("webservices").path("vrste").path("vrsta").path(rs.getString("Tip_idTip")).accept(MediaType.APPLICATION_XML).get(Vrsta.class);
			p.setMarket(DAOFactory.getMarketDAO().selectMarket(rs.getInt("Market_idMarket")));
			poslovnice.add(p);
		}
		return poslovnice;
	}

	public int deletePoslovnica(int id) throws Exception {
		qDelete.setInt(1, id);
		return qDelete.executeUpdate();
	}
}
